package org.op;

import org.op.service.ServiceManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class OpMain {
	
	ComponentView opFrame;			  // top level frame for the view components
	ServiceManager serviceManager;      // groups the services
	ControlManager controlManager;  // groups all of the component controllers
	
	private void init() {
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		
		opFrame = (ComponentView)context.getBean("opFrameView");
		serviceManager = (ServiceManager)context.getBean("services");
		controlManager = (ControlManager)context.getBean("controls");
	}

	/**
	 * 
	 *  TODO - Send this to the Comfiguration manager
	 * 
	 */
    private void configureGUI() {
    	serviceManager.getUiService().configureUI();
    }

    /**
     * Build the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     * 
     */
    private void createAndShowGUI() {
    	opFrame.build();
    }
    
    /**
     *  Start any services (NOTE: This could be handled by Spring, via the init handle)
     * 
     */
	public void startupServices() {
		serviceManager.startupServices();
	}

    public static void main(String[] args) {
    	
    	System.out.println("Starting OpMain..");
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            	OpMain application = new OpMain();
            	
            	application.init();  // instantiate components using dependency injection (Spring or Guice)
            	
            	application.startupServices();
            	
            	application.configureGUI();  // configure the look and feel
            	
            	application.createAndShowGUI();  // build the components

            }
        });
    }

	
}
